package com.smile.controller;

import cn.hutool.core.bean.BeanUtil;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.request.*;
import com.smile.service.FlyService;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/fly")
public class FlyController {

    @Autowired
    private FlyService flyService;

    @PostMapping("/flighttaskUndo")
    public R<Void> flighttaskUndo(@RequestBody FlighttaskUndoRequest flighttaskUndoRequest) throws MqttException {
        flyService.flighttaskUndo(BeanUtil.copyProperties(flighttaskUndoRequest, FlighttaskUndoRequestDTO.class));
        return R.ok();
    }

    /**
     * 飞往目标点
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/flyToPoint")
    public R<Void> flyToPoint(@RequestBody FlyToPointRequest flyToPointRequest) throws MqttException {
        flyService.flyToPoint(BeanUtil.copyProperties(flyToPointRequest, FlyToPointRequestDTO.class));
        return R.ok();
    }

    /**
     * 一键起飞
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/takeoffToPoint")
    public R<Void> takeoffToPoint(@RequestBody TakeOffToPointRequest takeOffToPointRequest) throws MqttException {
        System.out.println("一键起飞");
        flyService.takeoffToPoint(BeanUtil.copyProperties(takeOffToPointRequest, TakeOffToPointRequestDTO.class));
        System.out.println(takeOffToPointRequest);
        System.out.println("一键起飞");
        return R.ok();
    }

    /**
     * 飞行控制权抢夺
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/flightAuthorityGrab")
    public R<Void> flightAuthorityGrab(@RequestBody FlightAuthorityGrabRequest flightAuthorityGrabRequest) throws MqttException {
        flyService.flightAuthorityGrab(BeanUtil.copyProperties(flightAuthorityGrabRequest, FlightAuthorityGrabRequestDTO.class));
        return R.ok();
    }

    /**
     * 进入指令飞行控制模式 作废
     *
     * @return
     * @throws MqttException
     */
    @Deprecated
    @PostMapping("/drcModeEnter")
    public R<Void> drcModeEnter(@RequestBody DrcModeEnterRequest drcModeEnterRequest) throws MqttException {
        flyService.drcModeEnter(BeanUtil.copyProperties(drcModeEnterRequest, DrcModeEnterRequestDTO.class));
        return R.ok();
    }

    /**
     * 杆量控制
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/stickControl")
    public R<Void> stickControl(@RequestBody StickControlRequest stickControlRequest) throws MqttException {
        flyService.stickControl(BeanUtil.copyProperties(stickControlRequest, StickControlRequestDTO.class));
        return R.ok();
    }

    /**
     * 飞行控制
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/droneControl")
    public R<Void> droneControl(@RequestBody DroneControlRequest droneControlRequest) throws MqttException {
        flyService.droneControl(BeanUtil.copyProperties(droneControlRequest, DroneControlRequestDTO.class));
        return R.ok();
    }

    /**
     * 一键返航
     *
     * @return
     * @throws MqttException
     */
    @PostMapping("/returnHome")
    public R<Void> returnHome(@RequestBody ReturnHomeRequest returnHomeRequest) throws MqttException {
        flyService.returnHome(BeanUtil.copyProperties(returnHomeRequest, ReturnHomeRequestDTO.class));
        return R.ok();
    }

    /**
     * 更新 flyto 目标点
     *
     * @param flyToPointUpdateRequest
     * @return
     * @throws MqttException
     */
    @PostMapping("/flyToPointUpdate")
    public R<Void> flyToPointUpdate(@RequestBody FlyToPointUpdateRequest flyToPointUpdateRequest) throws MqttException {
        System.out.println("更新目标点Device");
        System.out.println(flyToPointUpdateRequest);
        FlyToPointUpdateRequestDTO flyToPointUpdateRequestDTO = BeanUtil.copyProperties(flyToPointUpdateRequest, FlyToPointUpdateRequestDTO.class);
        flyService.flyToPointUpdate(flyToPointUpdateRequestDTO);

        return R.ok();
    }


    @PostMapping("/flyToPointStop")
    public R<Void> flyToPointStop() {
        return R.ok();
    }

    /**
     * 飞行器急停
     *
     * @param droneEmergencyStopRequest
     * @return
     * @throws MqttException
     */
    @PostMapping("/droneEmergencyStop")
    public R<Void> droneEmergencyStop(@RequestBody DroneEmergencyStopRequest droneEmergencyStopRequest) throws MqttException {
        flyService.droneEmergencyStop(BeanUtil.copyProperties(droneEmergencyStopRequest, DroneEmergencyStopRequestDTO.class));
        return R.ok();
    }
}
